package com.nest.WorkNest.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.nest.WorkNest.R;
import com.nest.WorkNest.tools.ParseUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductSelectAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<HashMap<String, String>> datalist = null;
    private List<Map<String, Object>> mData;// 存储的EditText值
    public Map<String, String> editorValue = new HashMap<String, String>();//

    public ProductSelectAdapter(Context context, ArrayList<HashMap<String, String>> datalist) {
        this.context = context;
        this.datalist = datalist;
        init();
    }

    private void init() {
        editorValue.clear();
    }

    @Override
    public int getCount() {
        return datalist.size();
    }

    @Override
    public Object getItem(int position) {
        return datalist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.items_listview_orderedit, null);
            holder.tvOrdereditStyle = (TextView) convertView.findViewById(R.id.tv_orderedit_style);
            holder.btnOrdereditPlus = (Button) convertView.findViewById(R.id.btn_orderedit_plus);
            holder.btnOrdereditSub = (Button) convertView.findViewById(R.id.btn_orderedit_sub);
            holder.edtTxtOrdereditNumber = (EditText) convertView.findViewById(R.id.edtTxt_orderedit_number);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (!TextUtils.isEmpty(datalist.get(position).get("number"))) {
            holder.edtTxtOrdereditNumber.setText(datalist.get(position).get("number"));
        }
        if (TextUtils.isEmpty(datalist.get(position).get("style"))) {
            holder.tvOrdereditStyle.setText(datalist.get(position).get("color") + "/" + datalist.get(position).get("size"));
        } else {
            holder.tvOrdereditStyle.setText(datalist.get(position).get("style"));
        }

        holder.btnOrdereditPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                String sale = holder.edtTxtOrdereditNumber.getText().toString();
                int salenum = ParseUtils.string2int(sale);
                if (salenum < 99999) {
                    salenum += 1;
                } else {
                    Toast.makeText(context, "无法增加数量", Toast.LENGTH_SHORT).show();
                }
                holder.edtTxtOrdereditNumber.setText("" + salenum);
            }
        });
        holder.btnOrdereditSub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                String sale = holder.edtTxtOrdereditNumber.getText().toString();
                int salenum = ParseUtils.string2int(sale);
                if (salenum > 0) {
                    salenum -= 1;
                } else {
                    Toast.makeText(context, "无法减少数量", Toast.LENGTH_SHORT).show();
                }
                holder.edtTxtOrdereditNumber.setText("" + salenum);
            }
        });
        holder.edtTxtOrdereditNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = s.toString();
                int len = s.toString().length();
                if (len > 1 && text.substring(0, 1).equals("0")) {
                    holder.edtTxtOrdereditNumber.setText(text.substring(1));
                }
                if (TextUtils.isEmpty(text)) {
                    holder.edtTxtOrdereditNumber.setText("0");
                }

                holder.edtTxtOrdereditNumber.setSelection(holder.edtTxtOrdereditNumber.getText().length());
            }
        });
        holder.edtTxtOrdereditNumber.setSelection(holder.edtTxtOrdereditNumber.getText().length());
        return convertView;
    }

    public void update(ArrayList<HashMap<String, String>> datalist) {
        this.datalist = datalist;
        notifyDataSetChanged();
    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'items_listview_orderedit.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        TextView tvOrdereditStyle;
        Button btnOrdereditSub;
        EditText edtTxtOrdereditNumber;
        Button btnOrdereditPlus;
    }

}
